﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Activities.Presentation.Model;
using System.Activities;
using Microsoft.VisualBasic.Activities;

namespace TransactionServer.ActivityDesign.Helpers
{
    class ObjectArrayVariableConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            ModelItem modelItem = value as ModelItem;
            if (value != null)
            {

                if (modelItem.GetCurrentValue() is OutArgument<object[]>)
                {
                    OutArgument<object[]> outArgument = modelItem.GetCurrentValue() as OutArgument<object[]>;

                    if (outArgument != null)
                    {
                        Activity<Location<object[]>> expression = outArgument.Expression;
                        VisualBasicReference<object[]> vbexpression = expression as VisualBasicReference<object[]>;

                        if (vbexpression != null)
                        {
                            string result = vbexpression.ExpressionText;
                            result = result.Replace("{", "");
                            result = result.Replace("}", "");
                            return result;
                        }
                    }
                }
                else if (modelItem.GetCurrentValue() is InArgument<object[]>)
                {
                    InArgument<object[]> inArgument = modelItem.GetCurrentValue() as InArgument<object[]>;

                    if (inArgument != null)
                    {
                        Activity<object[]> expression = inArgument.Expression;
                        VisualBasicValue<object[]> vbexpression = expression as VisualBasicValue<object[]>;

                        if (vbexpression != null)
                        {
                            string result = vbexpression.ExpressionText;
                            result = result.Replace("{", "");
                            result = result.Replace("}", "");
                            return result;
                        }
                    }
                }

            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string arrObjects = value.ToString();
            StringBuilder sb = new StringBuilder();
            if (arrObjects.Length > 0)
            {
                sb.Append("{");
                sb.Append(arrObjects);
                sb.Append("}");
            }
            else
            {
                sb.Append(arrObjects);
            }

            if (targetType == typeof(OutArgument<object[]>))
            {
                VisualBasicReference<object[]> vbArgument = new VisualBasicReference<object[]>(sb.ToString());
                return new OutArgument<object[]>(vbArgument);
            }
            else if (targetType == typeof(InArgument<object[]>))
            {
                VisualBasicValue<object[]> vbArgument = new VisualBasicValue<object[]>(sb.ToString());
                return InArgument<object[]>.FromExpression(vbArgument);
            }

            return null;
        }
    }
}
